<?php
// @formatter:off
/**
 * @file ct.garbage.move.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-08-20 00:23:06 +0000 (Tue, 20 Aug 2013) $
 *
 * $Id: ct.garbage.move.php 102 2013-08-20 00:23:06Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0.0.2/cron/ct.garbage.move.php $
 */

$wcCronScripts["garbage.move"]	= array(
		"auto"		=> true,
		"enabled"	=> true,
		"name"		=> "Garbage to trashcan",
		"class"		=> "WCGarbageMove",
		"url"		=> "http://code.google.com/p/wcomix/"
);

$wcSpecialTags[] = WCGarbageMove::$TagName;

if(!isset($wcDefaults["class-WCGarbageMove"])) {
	$wcDefaults["class-WCGarbageMove"]	= array();
}
// @formatter:on

class WCGarbageMove extends WCCronTool {
	public static $TagName = WC_SPECIAL_TAG_TRASH;

	public function run($spacer = "") {
		global $wcGroupsHolder;
		global $wcDirectoriesHolder;
		global $wcTagsHolder;

		foreach($wcGroupsHolder->groups() as $group) {
			if(is_readable($group->root)) {
				echo "{$spacer}Checking group '{$group->name}' ({$group->id}):\n";

				if($group->trash) {
					$trashcan = @$wcDirectoriesHolder->itemByName($group->trash, $group);
					if($trashcan && $trashcan->ok()) {
						$trashTag = @$wcTagsHolder->itemByName(WCGarbageMove::$TagName, $group);
						if($trashTag && $trashTag->ok()) {
							foreach($trashTag->membersFull() as $comic) {
								if($comic->directory != $trashcan->id) {
									echo "{$spacer}\tMoving comic '{$comic->name}' ({$comic->id}) into trashcan: ";

									$futurePath = WCSanitizer::SystemPath("{$trashcan->fullPath()}/{$comic->name}");
									if(!file_exists($futurePath)) {
										$comic->setDirectory($trashcan);
										if($comic->directory == $trashcan->id) {
											echo WCColorText::ShellGreen("Done")."\n";
										} else {
											echo WCColorText::ShellRed("Failed")."\n";
										}
									} else {
										echo WCColorText::ShellRed("Failed").": Path '{$futurePath}' already exists\n";
									}
								}
							}
						} else {
							echo "{$spacer}\t".WCColorText::ShellRed("Failed").": Unable to load trashcan tag.\n";
						}
					} else {
						echo "{$spacer}\t".WCColorText::ShellRed("Failed").": Unable to load trashcan directory.\n";
					}
				} else {
					echo "{$spacer}\t".WCColorText::ShellYellow("Ignored").": This group has no trashcan.\n";
				}
			}
		}
	}
	public function showHelp($spacer = "") {
		echo "{$spacer}This tool walks through every comic tagged with '".WCGarbageMove::$TagName."'\n";
		echo "{$spacer}and moves it into group's trashcan.\n";
	}
	protected function loadOptions() {
		$this->_optionsLoaded = true;
	}
}
?>